﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using MongoDB.Bson;
using MongoDB.Driver;

namespace AnesSystem.Services
{
    public class PatientVitalSignsServicie : BaseService<PatientVitalSignsServicie, PatientVitalSignsModel>
    {
        /// <summary>
        /// 获取一段时间内的单项生命体征
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="code"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<PatientVitalSignsModel> GetByTimeACode(string pid, string code, DateTime startTime, DateTime endTime)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("PatientID", pid),
                                                          filter.Eq("Code", code),
                                                          filter.Gte("CaptureTime", startTime),
                                                          filter.Lte("CaptureTime", endTime));
            return FindAll(query).ToList();
        }


        /// <summary>
        /// 获取某个时间点的单个体征
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="code"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public PatientVitalSignsModel GetVitalSign(string pid, string code, DateTime time)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("PatientID", pid),
                                                          filter.Eq("Code", code),
                                                          filter.Eq("CaptureTime", time));
            List<PatientVitalSignsModel> ret = (List<PatientVitalSignsModel>)FindAll(query);
            if (ret != null && ret.Count != 0)
            {
                return ret[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 去一段时间内所有的体征数据
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<PatientVitalSignsModel> GetVitalByTime(string pid,DateTime startTime,DateTime endTime)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("PatientID", pid),
                                                          filter.Gte("CaptureTime", startTime),
                                                          filter.Lte("CaptureTime", endTime));
            return FindAll(query).ToList();
        }

        /// <summary>
        /// 取某个人某个时间点之后的体征
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="code"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<PatientVitalSignsModel> GetByTimeACode(string pid, string code, DateTime startTime)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("PatientID", pid),
                                                          filter.Eq("Code", code),
                                                          filter.Gte("CaptureTime", startTime));
            return FindAll(query).ToList();
        }

        /// <summary>
        /// 取某个人的体征
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="code"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<PatientVitalSignsModel> GetByPatientID(string pid)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Eq("PatientID", pid));
            return FindAll(query).ToList();
        }

        /// <summary>
        /// 取某个时间点之后的体征
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="code"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<PatientVitalSignsModel> GetByCaptureTime(DateTime time)
        {
            var filter = Builders<BsonDocument>.Filter;
            var query = Builders<BsonDocument>.Filter.And(filter.Gte("CaptureTime", time));
            return FindAll(query).ToList();
        }
    }
}
